package com.literature.search.core.utils.sp;

import com.literature.search.core.config.mongo.MongoConfig;
import cn.dev33.satoken.stp.StpUtil;
import com.literature.search.core.entity.User;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @Author: DangKong
 * @Date: 2023/3/1 23:08
 * @Description:
 */

public class SpUserUtil {

    private static MongoTemplate mongoTemplate;

    public static void setMongoTemplate() {
        SpUserUtil.mongoTemplate = MongoConfig.mongoTemplate;
    }
    public static User getCurrUser() {
        if (mongoTemplate == null){
            setMongoTemplate();
        }
        String userId = StpUtil.getLoginIdAsString(); // 从token中获取Id
        System.out.println("userId = " + userId);
        System.out.println("mongoTemplate = " +  mongoTemplate);
        Query query = new Query(Criteria.where("_id").is(userId));
        return mongoTemplate.findOne(query, User.class, "user");
    }



}
